﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;
using System.Windows.Forms;
using System.IO;

namespace Calendar.Domain
{
    public class FileIO
    {
        string WORK_FILE_PATH = "./lua/worklist.lua";
        string RECORD_FILE_PATH = "./lua/recordlist.lua";
        Lua _lua = new Lua();

        private LuaTable GetTable(string filepath, string tableName)
        {
            try
            {
                _lua.DoFile(filepath);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            LuaTable table = _lua.GetTable(tableName);
            return table;
        }

        public List<Work> LoadWork()
        {
            LuaTable workTable = GetTable(WORK_FILE_PATH, "workTable");
            List<Work> workList = new List<Work>();

            if (workTable != null)
            {
                foreach (LuaTable workInfo in workTable.Values)
                {
                    Work work = new Work();
                    work.Name = workInfo["name"].ToString();
                    work.StartTime = Convert.ToDateTime(workInfo["startTime"]);
                    work.Deadline = Convert.ToDateTime(workInfo["deadline"]);
                    work.EmergencyLevel = Convert.ToInt32(workInfo["emergancyLevel"]);
                    work.Progress = Convert.ToInt32(workInfo["progress"]);
                    workList.Add(work);
                }
            }
            return workList;
        }

        public void SaveWork(Dictionary<int, Work> workDictionary)
        {
            StreamWriter streamWriter = new StreamWriter(WORK_FILE_PATH, false);

            streamWriter.WriteLine("workTable = ");
            streamWriter.WriteLine("{");

            foreach (KeyValuePair<int, Work> work in workDictionary)
            {                              //[\"id\"] = \"" + work.Key + "\", 
                streamWriter.WriteLine(
                    "    {[\"name\"] = \"" + work.Value.Name +
                    "\", [\"startTime\"] = \"" + work.Value.StartTime.ToShortDateString() +
                    "\", [\"deadline\"] = \"" + work.Value.Deadline.ToShortDateString() +
                    "\", [\"emergancyLevel\"] = \"" + work.Value.EmergencyLevel +
                    "\", [\"progress\"] = \"" + work.Value.Progress + "\" },");
            }
            streamWriter.WriteLine("}");
            streamWriter.Close();
        }

        public List<WorkRecord> LoadRecord(string name)
        {
            LuaTable recordTable = GetTable(RECORD_FILE_PATH, "recordTable");
            List<WorkRecord> workRecordList = new List<WorkRecord>();

            if (recordTable != null)
            {
                foreach (LuaTable record in recordTable.Values)
                {
                    if (record["name"].ToString() == name)
                    {
                        WorkRecord workRecord = new WorkRecord();
                        workRecord.Name = record["name"].ToString();
                        workRecord.StartTime = Convert.ToDateTime(record["startTime"]);
                        workRecord.EndTime = Convert.ToDateTime(record["endTime"]);
                        workRecord.TotalTime = Convert.ToDouble(record["totalTime"]);
                        workRecord.TimeSlot = Convert.ToInt32(record["timeSlot"]);
                        workRecord.BreakTime = Convert.ToInt32(record["breakTime"]);
                        workRecordList.Add(workRecord);
                    }
                }
            }
            return workRecordList;
        }

        public void SaveRecord(List<WorkRecord> workRecordList)
        {
            StreamWriter streamWriter = new StreamWriter(RECORD_FILE_PATH, false);

            streamWriter.WriteLine("recordTable = ");
            streamWriter.WriteLine("{");

            foreach (WorkRecord record in workRecordList)
            {
                streamWriter.WriteLine(
                    "    {[\"name\"] = \"" + record.Name +
                    "\", [\"startTime\"] = \"" + record.StartTime.ToShortDateString() + " " + record.StartTime.ToString("HH:mm") +
                    "\", [\"endTime\"] = \"" + record.EndTime.ToShortDateString() + " " + record.EndTime.ToString("HH:mm") +
                    "\", [\"totalTime\"] = \"" + record.TotalTime.ToString("0.0") +
                    "\", [\"timeSlot\"] = \"" + record.TimeSlot +
                    "\", [\"breakTime\"] = \"" + record.BreakTime + "\" },");
            }
            streamWriter.WriteLine("}");
            streamWriter.Close();
        }
    }
}
